<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card">
      <div class="query-card">
        <el-form ref="query" :model="query" label-width="100px">
          <el-form-item style="margin-bottom:0px" label="奖品名称">
            <i slot="label" class="el-icon-search" style="font-size:20px"></i>
            <el-input v-model="query.keywords" style="width:360px" placeholder="请输入奖品名称"></el-input>
          </el-form-item>
          <el-form-item label="价格区间">
            <el-input v-model="query.lowPrice" style="width:200px" placeholder="低"></el-input>-
            <el-input v-model="query.heightPrice" style="width:200px" placeholder="高"></el-input>
          </el-form-item>
          <el-form-item style="margin-bottom:0px" label="奖品分类">
            <el-cascader
              v-model="query.class_id"
              :options="options"
              :props="{ checkStrictly: true,value:'id',label:'class_name' }"
              placeholder="请选择分类"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item style="margin-bottom:0px">
            <el-button type="primary" @click="queryAsk">筛选</el-button>
            <el-button @click="queryReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <!-- 表格 -->
    <el-card class="box-card">
      <img
        class="watermark"
        v-if="baseApi== 'https://testouhun.wm2177.com'"
        style="right: 0;"
        src="@/assets/watermark.png"
      />
      <el-button type="primary" style="margin-bottom:20px" @click="addList">添加</el-button>
      <el-button type="primary" style="margin-bottom:20px" @click="addBatch">批量添加</el-button>
      <el-button type="primary" style="margin-bottom:20px" @click="batchEdite=true">批量编辑</el-button>
      <el-button
        type="primary"
        style="margin-bottom:20px"
        :disabled="!batchEdite"
        @click="batchEditeSub"
      >确认编辑</el-button>
      <el-button
        type="primary"
        style="margin-bottom:20px"
        :disabled="!batchEdite"
        @click="cancelBatch"
      >取消编辑</el-button>
      <el-table
        @sort-change="sortChange"
        :data="tableData"
        :header-cell-style="{background:'#eee'}"
        border
        v-loading="loading"
        style="width: 100%;margin-bottom: 20px;"
      >
        <el-table-column align="center" prop="id" label="ID"></el-table-column>
        <el-table-column align="center" prop="goods_name" label="奖品名称"></el-table-column>
        <el-table-column align="center" label="奖品图片">
          <template slot-scope="scope">
            <el-popover placement="top-start" title trigger="hover">
              <img :src="scope.row.img" alt style="max-width:150px" />
              <!-- <el-avatar shape="square" :size="150" fit="cover" :src="scope.row.img"></el-avatar> -->
              <el-avatar
                slot="reference"
                shape="square"
                :size="50"
                fit="scale-down"
                :src="scope.row.img"
              ></el-avatar>
              <!-- <img
                slot="reference"
                :src="scope.row.img"
                style="width: 60px;height: 60px"
              />-->
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="class_name" label="分类"></el-table-column>
        <el-table-column align="center" prop="y_price" label="成本">
          <template slot-scope="scope">
            <div v-if="!batchEdite">{{scope.row.y_price}}</div>
            <div v-else>
              <el-input v-model="scope.row.y_price"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="price" label="价格" sortable="custom">
          <template slot-scope="scope">
            <div v-if="!batchEdite">{{scope.row.price}}</div>
            <div v-else>
              <el-input v-model="scope.row.price"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="address" label="预售/现货">
          <template slot-scope="scope">
            <div v-if="scope.row.type==1&&!batchEdite">预售</div>
            <div v-if="scope.row.type==2&&!batchEdite">现货</div>
            <el-select v-if="batchEdite" v-model="scope.row.type" placeholder="请选择是否预售">
              <el-option label="预售" value="1"></el-option>
              <el-option label="现货" value="2"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="address" label="是否参与合成材料">
          <template slot-scope="scope">
            <div v-if="scope.row.is_compound==1&&!batchEdite">可以</div>
            <div v-if="scope.row.is_compound==0&&!batchEdite">不可以</div>
            <el-select v-if="batchEdite" v-model="scope.row.is_compound" placeholder="请选择参与合成材料">
              <el-option label="可以" value="1"></el-option>
              <el-option label="不可以" value="0"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="compound_price" label="合成数值">
          <template slot-scope="scope">
            <div v-if="!batchEdite">{{scope.row.compound_price}}</div>
            <div v-else>
              <el-input v-model="scope.row.compound_price"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="is_by" label="是否包邮">
          <template slot-scope="scope">
            <div v-if="scope.row.is_by==1&&!batchEdite">是</div>
            <div v-if="scope.row.is_by==0&&!batchEdite">否</div>
            <el-select v-if="batchEdite" v-model="scope.row.is_by" placeholder="请选择是否包邮">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="0"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="handleEdite(scope.row)" type="text" size="small">编辑</el-button>
            <el-popconfirm
              confirmButtonText="好的"
              cancelButtonText="不用了"
              icon="el-icon-info"
              iconColor="red"
              title="这是一段内容确定删除吗？"
              @onConfirm="handleDel(scope.row)"
            >
              <el-button
                style="margin-left:10px;color:red"
                slot="reference"
                type="text"
                size="small"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 抽屉 -->
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="margin-left:50px">
        <el-form-item label="选择商品">
          <el-button type="primary" @click="chooseGood">选择商品</el-button>
        </el-form-item>
        <el-form-item label="商品名称" prop="goods_name">
          <el-input style="width:360px" v-model="form.goods_name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="上传图片" prop="img">
          <el-upload
            class="avatar-uploader"
            action="https://upload-z0.qiniup.com/"
            :data="postData"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="奖品分类" prop="class_id">
          <el-cascader
            style="width:360px"
            v-model="form.class_id"
            :options="options"
            :props="{ checkStrictly: true,value:'id',label:'class_name' }"
            placeholder="请选择分类"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="参与合成材料" prop="is_compound">
          <el-select style="width:360px" v-model="form.is_compound" placeholder="请选择获是否参与合成材料">
            <el-option label="可以" value="1"></el-option>
            <el-option label="不可以" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合成数值" prop="compound_price">
          <el-input
            style="width:360px"
            :disabled="form.is_compound==0"
            v-model="form.compound_price"
            placeholder="请输入合成数值"
          ></el-input>
        </el-form-item>
        <el-form-item label="售价" prop="price">
          <el-input style="width:360px" v-model="form.price" placeholder="请输入售价"></el-input>
        </el-form-item>
        <el-form-item label="成本" prop="y_price">
          <el-input style="width:360px" v-model="form.y_price" placeholder="请输入原价"></el-input>
        </el-form-item>
        <el-form-item label="预售/现货" prop="type">
          <el-select style="width:360px" v-model="form.type" placeholder="请选择获预售/现货">
            <el-option label="预售" value="1"></el-option>
            <el-option label="现货" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label prop>
          <div style="color:red">请勿将现货奖品改成预售</div>
        </el-form-item>
        <el-form-item label="是否包邮" prop="is_by">
          <el-select style="width:360px" v-model="form.is_by" placeholder="请选择是否包邮">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否保底" prop="is_bd">
          <el-select style="width:360px" v-model="form.is_bd" placeholder="请选择是否保底">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input
            style="width:360px"
            type="textarea"
            v-model="form.details"
            placeholder="请输入商品详情介绍"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确定</el-button>
          <el-button @click="subCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <!-- 商品库 -->
    <el-dialog
      title="选择商品"
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="goodhandleClose"
    >
      <div class="query-card">
        <!-- <el-row type="flex" align="middle">
        <el-col :span="12">-->
        <div class="grid-content bg-purple search">
          <el-form ref="goodquery" :model="goodquery" label-width="100px">
            <el-form-item style="margin-bottom:0px" label="商品名称">
              <el-input style="width:360px" v-model="goodquery.keywords" placeholder="请输入商品名称"></el-input>
            </el-form-item>
            <el-form-item style="margin-bottom:0px" label="分类">
              <el-cascader
                v-model="goodquery.class_id"
                :options="goodoptions"
                :props="{ checkStrictly: true,value:'id',label:'class_name' }"
                placeholder="请选择分类"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-form>
        </div>
        <!-- </el-col> -->
        <!-- <el-col :span="12"> -->
        <div class="grid-content bg-purple-light">
          <el-button type="primary" @click="goodqueryAsk">查询</el-button>
          <el-button @click="goodqueryReset">重置</el-button>
        </div>
        <!-- </el-col>
        </el-row>-->
      </div>
      <el-table
        :data="goodsData"
        ref="sendmultipleTable"
        border
        tooltip-effect="dark"
        style="width: 100%;margin: 20px 0;"
        @row-click="handleSelectionChange"
        :header-cell-style="{background:'#eee'}"
      >
        <el-table-column align="center" width="55">
          <template slot-scope="scope">
            <el-radio v-model="sendForm.goods_id" :label="scope.row.id">{{""}}</el-radio>
          </template>
        </el-table-column>
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>

        <el-table-column align="center" prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column align="center" label="商品图片">
          <template slot-scope="scope">
            <el-popover placement="top-start" title trigger="hover">
              <img :src="scope.row.img" alt style="max-width:150px" />
              <!-- <el-avatar shape="square" :size="150" fit="cover" :src="scope.row.img"></el-avatar> -->
              <el-avatar
                slot="reference"
                shape="square"
                :size="50"
                fit="scale-down"
                :src="scope.row.img"
              ></el-avatar>
              <!-- <img
                slot="reference"
                :src="scope.row.img"
                style="width: 60px;height: 60px"
              />-->
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="spec" label="规格"></el-table-column>
        <el-table-column align="center" prop="class_name" label="分类/品牌"></el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="goodcurrentPage"
          :total="goodtotal"
          @current-change="goodcurrentChange"
        ></el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="goodsInfo">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 批量添加-->
    <el-dialog
      title="选择商品"
      :visible.sync="batchdialogVisible"
      width="60%"
      :before-close="batchgoodhandleClose"
    >
      <div class="query-card">
        <!-- <el-row type="flex" align="middle">
        <el-col :span="12">-->
        <div class="grid-content bg-purple search">
          <el-form ref="batchgoodquery" :model="goodquery" label-width="100px">
            <el-form-item style="margin-bottom:0px" label="商品名称">
              <el-input style="width:360px" v-model="goodquery.keywords" placeholder="请输入商品名称"></el-input>
            </el-form-item>
            <el-form-item style="margin-bottom:0px" label="分类">
              <el-cascader
                v-model="goodquery.class_id"
                :options="goodoptions"
                :props="{ checkStrictly: true,value:'id',label:'class_name' }"
                placeholder="请选择分类"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-form>
        </div>
        <!-- </el-col> -->
        <!-- <el-col :span="12"> -->
        <div class="grid-content bg-purple-light">
          <el-button type="primary" @click="batchgoodqueryAsk">查询</el-button>
          <el-button @click="batchgoodqueryReset">重置</el-button>
        </div>
        <!-- </el-col>
        </el-row>-->
      </div>
      <el-table
        :data="goodsData"
        ref="sendmultipleTable"
        border
        tooltip-effect="dark"
        style="width: 100%;margin: 20px 0;"
        @selection-change="batchhandleSelectionChange"
        :header-cell-style="{background:'#eee'}"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>

        <el-table-column align="center" prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column align="center" label="商品图片">
          <template slot-scope="scope">
            <el-popover placement="top-start" title trigger="hover">
              <img :src="scope.row.img" alt style="max-width:150px" />
              <!-- <el-avatar shape="square" :size="150" fit="cover" :src="scope.row.img"></el-avatar> -->
              <el-avatar
                slot="reference"
                shape="square"
                :size="50"
                fit="scale-down"
                :src="scope.row.img"
              ></el-avatar>
              <!-- <img
                slot="reference"
                :src="scope.row.img"
                style="width: 60px;height: 60px"
              />-->
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="spec" label="规格"></el-table-column>
        <el-table-column align="center" prop="class_name" label="分类/品牌"></el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="goodcurrentPage"
          :total="goodtotal"
          @current-change="goodcurrentChange"
        ></el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="batchgoodsInfo">确 定</el-button>
        <el-button @click="batchdialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/api/qiniu'
import { categoryList } from '@/api/product'
import {
  boxRewardList,
  boxRewardTypeList,
  addBoxReward,
  editBoxReward,
  delBoxReward
} from '@/api/boxreward'
import { productList } from '@/api/system'
export default {
  name: 'BoxRewardList',
  data() {
    return {
      baseApi: process.env.VUE_APP_BASE_API,
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //query
      query: {
        keywords: '',
        class_id: '0',
        lowPrice: '',
        heightPrice: '',
        order_by: ''
      },
      tableData: [],
      // 编辑抽屉
      drawer: false,
      direction: 'rtl',
      title: '编辑',
      form: {
        goods_name: '',
        img: '',
        class_id: '',
        is_compound: '',
        compound_price: '0',
        price: '',
        y_price: '',
        type: '2',
        is_by: '1',
        is_bd: '0',
        details: ''
      },
      rules: {
        goods_name: [
          { required: true, message: '请输入奖品名称', trigger: 'blur' }
        ],
        img: [{ required: true, message: '请选择图片', trigger: 'blur' }],
        class_id: [
          { required: true, message: '请选择奖品分类', trigger: 'blur' }
        ],
        is_compound: [
          { required: true, message: '请选择是否参与合成材料', trigger: 'blur' }
        ],
        compound_price: [
          { required: true, message: '请选输入合成数值', trigger: 'blur' }
        ],
        price: [{ required: true, message: '请选输入售价', trigger: 'blur' }],
        y_price: [{ required: true, message: '请选输入原价', trigger: 'blur' }],
        type: [
          { required: true, message: '请选择预售、现货', trigger: 'blur' }
        ],
        is_by: [{ required: true, message: '请选择是否包邮', trigger: 'blur' }],
        is_bd: [{ required: true, message: '请选择是否保底', trigger: 'blur' }],
        details: [
          { required: true, message: '请选输入奖品详情介绍', trigger: 'blur' }
        ]
      },
      // 上传图片
      url: '',
      imageUrl: '',
      postData: {
        token: ''
      },
      loading: false,
      options: [],
      optionLoading: false,
      priceArr: [],
      // 商品库
      dialogVisible: false,
      goodtotal: 0,
      goodcurrentPage: 1,
      goodpageSize: 10,
      goodquery: {
        keywords: '',
        class_id: ''
      },
      goodoptions: [],
      sendForm: {
        goods_id: ''
      },
      goods: '',
      goodsData: '',
      // 批量添加商品库
      batchdialogVisible: false,
      batchgoodquery: {
        keywords: '',
        class_id: ''
      },
      batchgoodoptions: [],
      batchgoods: [],
      batchgoodsData: '',
      //是否批量编辑
      batchEdite: false
    }
  },
  mounted() {
    this.getBoxRewardList()
    this.getBoxRewardTypeList()
    this.getToken()
  },
  methods: {
    //获取奖品列表
    async getBoxRewardList() {
      this.loading = true
      const res = await boxRewardList({
        page: this.currentPage,
        limit: this.pageSize,
        class_id: this.query.class_id,
        price: this.priceArr,
        keywords: this.query.keywords,
        order_by: this.query.order_by
      })
      this.loading = false
      this.total = res.count
      this.tableData = res.data
      console.log(res)
    },
    //获取奖品分类列表
    async getBoxRewardTypeList() {
      this.optionLoading = true
      const res = await categoryList({
        page: '1',
        limit: '999',
        keywords: ''
      })
      this.optionLoading = false
      this.options = res.data
      console.log(res)
    },
    // 搜索列表
    queryAsk() {
      // 不可以一个有值一个没值
      if (!this.query.lowPrice && !this.query.heightPrice) {
        console.log('meiy')
        console.log(this.query)
        this.priceArr = []
        this.currentPage = 1
        if (typeof this.query.class_id != 'string') {
          this.query.class_id =
            this.query.class_id[this.query.class_id.length - 1]
        }
        this.getBoxRewardList()
      } else if (this.query.lowPrice && this.query.heightPrice) {
        console.log('you')
        this.priceArr = [this.query.lowPrice, this.query.heightPrice]
        console.log(this.query)
        if (typeof this.query.class_id != 'string') {
          this.query.class_id =
            this.query.class_id[this.query.class_id.length - 1]
        }
        this.currentPage = 1
        this.getBoxRewardList()
      } else if (!this.query.lowPrice || !this.query.heightPrice) {
        this.$message.error('请输入价格区间')
      }
    },
    queryReset() {
      this.resetForm('query')
      this.total = 0
      this.currentPage = 1
      this.pageSize = 10
      this.query.keywords = ''
      this.query.class_id = '0'
      this.query.lowPrice = ''
      this.query.heightPrice = ''
      this.priceArr = []
      // this.query.order_by = "";
      this.getBoxRewardList()
    },
    async addList() {
      this.drawer = true
      this.title = '添加'
      await this.getBoxRewardTypeList()
      this.form.class_id = this.tableData[0].class_id
    },
    // 表格操作
    async handleDel(row) {
      const res = await delBoxReward({ id: row.id })
      console.log(res)
      if (res.st == 1) {
        this.getBoxRewardList()
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      } else {
        this.$message.error('删除失败')
      }
    },
    handleEdite(row) {
      this.drawer = true
      this.title = '编辑'
      console.log(row)
      this.form.goods_name = row.goods_name
      this.form.class_id = row.class_id
      this.form.img = row.img
      this.imageUrl = row.img
      this.form.is_compound = row.is_compound
      this.form.compound_price = row.compound_price
      this.form.price = row.price
      this.form.y_price = row.y_price
      this.form.type = row.type
      this.form.details = row.details
      this.form.is_by = row.is_by
      this.form.is_bd = row.is_bd
      this.form.id = row.id
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current
      this.getBoxRewardList()
    },
    //抽屉
    handleClose(done) {
      this.resetForm('form')
      const form = {
        goods_name: '',
        img: '',
        class_id: '',
        is_compound: '',
        compound_price: '0',
        price: '',
        y_price: '',
        type: '2',
        details: '',
        is_by: '1',
        is_bd: '0'
      }
      this.imageUrl = ''
      this.form = form
      this.drawer = false
      done()
    },
    subCancel() {
      this.resetForm('form')
      const form = {
        goods_name: '',
        img: '',
        class_id: '',
        is_compound: '',
        compound_price: '0',
        price: '',
        y_price: '',
        type: '2',
        details: '',
        is_by: '1',
        is_bd: '0'
      }
      this.imageUrl = ''
      this.form = form
      this.drawer = false
    },
    onSubmit() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          if (this.title === '添加') {
            console.log(this.form)
            const res = await addBoxReward({ ...this.form })
            console.log(res)
            if (res.st === 1) {
              this.resetForm('form')
              const form = {
                goods_name: '',
                img: '',
                class_id: '',
                is_compound: '',
                compound_price: '0',
                price: '',
                y_price: '',
                type: '2',
                details: '',
                is_by: '1',
                is_bd: '0'
              }
              this.imageUrl = ''
              this.form = form
              this.getBoxRewardList()
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.drawer = false
            } else {
              console.log(res)
              this.$message.error('添加失败')
            }
          } else if (this.title === '编辑') {
            const res = await editBoxReward({ ...this.form })
            console.log(res)
            if (res.st === 1) {
              this.resetForm('form')
              const form = {
                goods_name: '',
                img: '',
                class_id: '',
                is_compound: '',
                compound_price: '0',
                price: '',
                y_price: '',
                type: '2',
                details: '',
                id: '',
                is_by: '1',
                is_bd: '0'
              }
              this.imageUrl = ''
              this.form = form
              this.getBoxRewardList()
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
              this.drawer = false
            } else {
              console.log(res)
              this.$message.error(res.msg)
            }
          }
        }
      })
    },
    // 上传图片
    async getToken() {
      const res = await getToken()
      this.postData.token = res.data.uptoken
      this.url = res.data.domain
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = this.url + res.key
      this.form.img = this.imageUrl
    },
    beforeAvatarUpload(file) {
      //   const isJPG = file.type === "image/jpeg";
      //   if (!isJPG) {
      //     this.$message.error("上传头像图片只能是 JPG 格式!");
      //   }
      const isLt1M = file.size / 1024 / 1024 < 1

      if (!isLt1M) {
        this.$message.error('上传头像图片大小不能超过 1MB!')
        return
      }
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current
      this.getBoxRewardList()
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 排序
    sortChange({ column, prop, order }) {
      if (order == 'descending') {
        this.query.order_by = prop + ',' + 'desc'
        this.getBoxRewardList()
      } else if (order == 'ascending') {
        this.query.order_by = prop + ',' + 'asc'
        this.getBoxRewardList()
      }
    },
    // 选择商品
    async chooseGood() {
      await this.getProductList()
      this.getCategary()
      this.dialogVisible = true
    },
    // 商品库
    async getProductList() {
      const res = await productList({
        page: this.goodcurrentPage,
        limit: this.goodpageSize,
        keywords: this.goodquery.keywords,
        class_id: this.goodquery.class_id
      })
      this.goodsData = res.data
      this.goodtotal = res.count
    },
    goodhandleClose(done) {
      done()
    },
    // 获取商品分类
    async getCategary() {
      const res = await categoryList({ page: 1, limit: 999, keywords: '' })
      console.log(res)
      this.goodoptions = res.data
    },
    // 搜索列表
    goodqueryAsk() {
      console.log(this.goodquery)
      this.goodquery.class_id =
        this.goodquery.class_id[this.goodquery.class_id.length - 1]
      this.goodcurrentPage = 1
      this.getProductList()
    },
    goodqueryReset() {
      this.resetForm('goodquery')
      this.goodtotal = 0
      this.goodcurrentPage = 1
      this.goodpageSize = 10
      this.goodquery.keywords = ''
      this.goodquery.class_id = ''
      this.getProductList()
    },
    //分页改变
    goodcurrentChange(current) {
      this.goodcurrentPage = current
      this.batchgoods = []
      this.getProductList()
    },
    handleSelectionChange(val) {
      this.goods = val
      this.sendForm.goods_id = val.id
    },
    goodsInfo() {
      console.log(this.goods)
      this.form.goods_name = this.goods.goods_name
      this.form.spec = this.goods.spec
      this.form.img = this.goods.img
      this.form.class_id = this.goods.class_id
      this.imageUrl = this.goods.img
      this.dialogVisible = false
    },
    // 批量添加商品库
    async addBatch() {
      await this.getProductList()
      this.getCategary()
      this.batchdialogVisible = true
    },
    batchgoodhandleClose(done) {
      done()
    },
    // 搜索列表
    batchgoodqueryAsk() {
      console.log(this.batchgoodquery)
      this.goodquery.class_id =
        this.goodquery.class_id[this.goodquery.class_id.length - 1]
      this.goodcurrentPage = 1
      this.getProductList()
    },
    batchgoodqueryReset() {
      this.resetForm('batchgoodquery')
      this.goodtotal = 0
      this.goodcurrentPage = 1
      this.goodpageSize = 10
      this.goodquery.keywords = ''
      this.goodquery.class_id = ''
      this.getProductList()
    },
    //分页改变
    batchgoodcurrentChange(current) {
      this.goodcurrentPage = current
      this.getProductList()
    },
    batchhandleSelectionChange(val) {
      console.log(val)
      this.batchgoods = []
      val.forEach((item) => {
        let batchform = {
          goods_name: item.goods_name,
          img: item.img,
          class_id: item.class_id,
          is_compound: '0',
          compound_price: '0',
          price: '1',
          y_price: '1',
          type: '2',
          is_by: '1',
          is_bd: '0',
          details: '',
          spec: item.spec
        }
        this.batchgoods.push(batchform)
      })
      console.log(this.batchgoods)
    },
    async batchgoodsInfo() {
      let sure = []
      for (let i = 0; i < this.batchgoods.length; i++) {
        const element = this.batchgoods[i]
        const res = await addBoxReward({ ...element })
        if (res.st === 1) {
          this.batchdialogVisible = false
          sure.push(1)
        } else {
          console.log(res)
          sure.push(2)
        }
      }
      // this.batchgoods.forEach(async item => {
      //   const res = await addBoxReward({ ...item });
      //   console.log(res);
      // });

      if (!sure.includes(2)) {
        this.$message({
          message: '添加成功',
          type: 'success'
        })
        this.getBoxRewardList()
      } else {
        this.$message.error('添加失败')
        this.getBoxRewardList()
      }
    },

    // 批量编辑
    async batchEditeSub() {
      console.log(this.tableData)
      let sure = []

      for (let i = 0; i < this.tableData.length; i++) {
        const element = this.tableData[i]
        const res = await editBoxReward({ ...element })
        if (res.st === 1) {
          sure.push(1)
        } else {
          console.log(res)
          sure.push(2)
        }
      }

      if (!sure.includes(2)) {
        this.$message({
          message: '编辑成功',
          type: 'success'
        })
        this.batchEdite = false
        this.getBoxRewardList()
      } else {
        this.$message.error('编辑失败')
        this.batchEdite = false
        this.getBoxRewardList()
      }
    },
    // 取消批量编辑
    cancelBatch() {
      this.batchEdite = false
      this.getBoxRewardList()
    }
  }
}
</script>

<style lang='scss' scoped>
.query-card {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  ::v-deep .el-form {
    display: flex;
    flex-wrap: wrap;
  }
}
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-textarea__inner {
  width: 360px !important;
}
::v-deep .el-drawer.rtl {
  width: 650px !important;
  overflow: auto;
}
// 上传图片
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>